﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using BLL;
using DTO;

using DevExpress.XtraEditors;
using System.Data;

namespace CRM.GUI.QLKhachHang
{
    public partial class frmImport : DevExpress.XtraEditors.XtraForm
    {
        ExcelFunc func;
        public frmImport()
        {
            BLsupervisor sup = new BLsupervisor();
            ImportHandler import = new ImportHandler();            
            InitializeComponent();
            txtImportDate.Text = HelperClass.SystemDateTime.ToString("dd/MM/yyyy");
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            func = new ExcelFunc();
            cbxSheetName.Items.Clear();
            progressBar.Value = 0;
            txtSourceName.Clear();
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                txtImport.Text = openFileDialog.FileName;
                if (!txtImport.Text.Contains("xls") || !txtImport.Text.Contains("xlsx"))
                {
                    MessageBox.Show("Chỉ nhận file Excel", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                else
                {
                    string message = "";
                    func.sGetExcelSheetNames(txtImport.Text, ref message);
                    String[] sExcelSheetName = func.sSheetName;
                    try
                    {
                        for (int i = 0; i < sExcelSheetName.Length; i++)
                        {
                                if (!string.IsNullOrEmpty(sExcelSheetName[i]))
                                    cbxSheetName.Items.Add(sExcelSheetName[i]);
                        }
                    }
                    catch
                    {
                        throw;
                    }
                    btnUpload.Enabled = true;
                }
            }
            else
            {
                return;
            }
            
        }

        private void btnUpload_Click(object sender, EventArgs e)
        {
            btnUpload.Enabled = false;
            func = new ExcelFunc();
            string message = "";            
            ExcelFunc.ReadProgress = new ExcelFunc.OnReadProgress(ShowProgress);
            func.ImportData(txtImport.Text, cbxSheetName.Text, ref message);
            if (message == "")
            {
                if (func.ErrorList.Count == 0)
                {
                    if (func.dtRaw.Rows.Count > 0)
                    {
                        DataTranferEntity trans = new DataTranferEntity();
                        trans.Source = new SourceEntity();
                        trans.Source.ImportDate = HelperClass.SystemDateTime;
                        trans.Source.SourceName = txtSourceName.Text;
                        MessageBox.Show("Thành công");
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show(message);
                    }
                }
                else
                {
                    func.dtRaw.Clear();
                    ExcelFunc.LoadDataToUI(func.dtRaw);
                    frmView frm = new frmView();
                    frm.ShowDialog();                    
                    progressBar.Value = 0;
                    txtSourceName.Text = "";
                }
            }
            else
            {
                MessageBox.Show(message);
                progressBar.Value = 0;
                txtSourceName.Text = "";
            }
            
                   
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            txtImport.Clear();
            txtSourceName.Clear();
            cbxSheetName.Items.Clear();
            cbxSheetName.Text = null;            
        }

        private void txtSourceName_TextChanged(object sender, EventArgs e)
        {
            if (txtSourceName.Text != "")
            {
                btnUpload.Enabled = true;
            }
            else
            {
                btnUpload.Enabled = false;
            }
        }

        private void ShowProgress(int progress)
        {
            progressBar.Value = progress;            
        }

        private void txtSourceName_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnUpload_Click(sender, e);
            }
        }

    }
}